/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var systemService = require('System_Service');
	var registerService = require('Register_Service');

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			WindowTools.preloadTemplate();
			UIInit();
			clickInit();
		});
	}
	/**
	 * ui的初始化
	 */
	function UIInit() {
		systemService.getDrivingTypes(function(data) {
			//			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.Value);
				option.innerHTML = item.Text;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('driveType');
			dataContainer.appendChild(fragment);
		});

		systemService.getSchoolList(function(data) {
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.id);
				option.innerHTML = item.name;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('schools');
			dataContainer.appendChild(fragment);
		})

		systemService.getClassType(function(data) {
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.Value);
				option.innerHTML = item.Text;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('classType');
			dataContainer.appendChild(fragment);
		})
	}

	/**
	 * 点击事件初始化
	 */
	function clickInit() {
		var commit = document.getElementById('commit'); //提交按钮
		var reset = document.getElementById('reset'); //提交按钮
		var history = document.getElementById('history');//历史按钮

		var account = document.getElementById('account');
		//性别选择
		var sex = document.getElementById('sex');
		var indexSex = sex.selectedIndex;
		var sexValue = sex.options[indexSex].value;

		var selfnum = document.getElementById('selfnum');
		var phone = document.getElementById('phone');
		var qq = document.getElementById('qq');
		var weixin = document.getElementById('weixin');
		var email = document.getElementById('email');
		var address = document.getElementById('address');
		var postcode = document.getElementById('postcode');

		history.addEventListener("tap",function(){
			WindowTools.openWinWithTemplate("","apply_history.html");
		});

		commit.addEventListener("tap", function() {
			//驾校选择
			var schools = document.getElementById('schools');
			var schoolIndex = schools.selectedIndex;
			var schoolValue = schools.options[schoolIndex].value;
			//驾照类型选择
			var driveType = document.getElementById('driveType');
			var driveIndex = driveType.selectedIndex;
			var driveValue = driveType.options[driveIndex].value;

			//课程类型选择
			var classType = document.getElementById('classType');
			var classIndex = classType.selectedIndex;
			var classValue = classType.options[classIndex].value;

			var data = {
				"Name": account.value,
				"Gender": sexValue,
				"CardNum": selfnum.value,
				"Contact": phone.value,
				"QQ": qq.value,
				"WeiXin": weixin.value,
				"Email": email.value,
				"Address": address.value,
				"ZipCode": postcode.value,
				"DrivingID": schoolValue,
				"DrivingLicenseType": driveValue,
				"ClassType": classValue,
				"ApplyType": "ChuanTong"
			};
			registerService.apply(data, function(data) {
				debugger;
			});
		})
	}
});